package com.xue.service;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class RibbonSkillService {

	
	@Autowired
    RestTemplate restTemplate;
	
	@HystrixCommand(fallbackMethod = "shopError")
    public Object getShop(String productId , String userId) {//kill-service
		
		//String rstr = restTemplate.getForObject("http://SKILLSERVICE/userShop?productId="+productId+"&userId="+userId,String.class);				
//		
		ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
		//List<String> list = new ArrayList<>();
		
		
	  	cachedThreadPool.execute(new Runnable(){
			@Override
			public void run() {
				System.out.println("******bibbon*****进入线程执行*************");
				 try {  
	                 Thread.sleep(1000);  
	             } catch (InterruptedException e) {  
	                 e.printStackTrace();  
	             }  
				String str = restTemplate.getForObject("http://SKILLSERVICE/userShop?productId="+productId+"&userId="+userId,String.class);				
				System.out.println("结果:"+str);
				//list.add(str);
			}	        	   
	     });  //创建 n 个线程。
		
		//
		 
//		 	Object rtnObj = null;
//	        Future result1 = cachedThreadPool.submit( new Callable() {
//	            @Override
//	            public String call() throws Exception {
//	                //int sum = 0;
//	                //Thread.sleep( 1000 );
//	                //System.out.println(sum+"22222222222222"+Thread.currentThread().getName());
//	            	String rstr = restTemplate.getForObject("http://SKILLSERVICE/userShop?productId="+productId+"&userId="+userId,String.class);				
//					
//	            	//String rstr = dubboService.getShop(pro, userId);
//	                System.err.println("结果:"+rstr);
//	            	return rstr;
//	            }
//
//	        });
//	        try {
//	        	 //m.put("rtn",result1.get());
//	        	rtnObj = result1.get();
//	        	
//			} catch (InterruptedException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			} catch (ExecutionException e) {
//				// TODO Auto-generated catch block
//				e.printStackTrace();
//			}
	        cachedThreadPool.shutdown(); 
		return "等待中";
    }

    public String shopError(String productId , String userId){
        return "错误：,"+userId+",沒有找到相对地址";
    }
}
